package com.lvbey.www;


//import org.apache.commons.lang.StringEscapeUtils;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Controller
public class IndexController {

    @RequestMapping("/index")
    public String upload() {
        return "index";
    }


    @PostMapping("/uploadFile")
    @ResponseBody
    public ResponseJson uploadFile(@RequestParam("file") MultipartFile file) {
        if(file.getSize() > 5*1024*1024){
            return ResponseJson.error("Upload file should not exceed 5M");
        }
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            ImageHandler imageHandler = new ImageHandler();
            String data = imageHandler.image2ArtAscii(inputStream, 3000);
            if(data == null){
                return ResponseJson.error( Constants.ERROR_MSG);
            }
            return ResponseJson.success(data);

        } catch (IOException e) {
            return ResponseJson.error( Constants.ERROR_MSG);
        }
    }

}
